package com.bjesc_b2c.app.appraiset.mvp.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bjesc_b2c.app.appraiset.R;
import com.bjesc_b2c.app.appraiset.adapter.UploadHistoryAdapter;
import com.bjesc_b2c.app.appraiset.base.BaseFragment;
import com.bjesc_b2c.app.appraiset.http.IApiActivityRequest;
import com.bjesc_b2c.app.appraiset.http.IApiFragmentRequest;
import com.bjesc_b2c.app.appraiset.mvp.presenter.CarBrandPersenter;
import com.bjesc_b2c.app.appraiset.widget.popupwindow.CarBrandPop;
import com.example.codeutils.utils.LogUtils;
import com.liaoinstan.springview.container.DefaultFooter;
import com.liaoinstan.springview.widget.SpringView;
import com.orhanobut.logger.Logger;
import com.rxretrofit_build.util.MapUtil;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;

import app.pinggushi.bjesc.com.baserecyclerview.adapter.MultiItemTypeAdapter;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import master_bean.SelectCarBrandBean;
import master_bean.ToDayBean;
import rxretrofit_base.model.ShowType;
import rxretrofit_base.model.TokenBean;
import rxretrofit_base.net.BaseSubscriber;
import rxretrofit_base.net.RxRequest;

import static com.bjesc_b2c.app.appraiset.R.id.tv_car_brand;
import static com.bjesc_b2c.app.appraiset.mvp.view.activity.BasicInfoActivity.brandId;

/**
 * Created by lenovo on 2017/2/27.
 */

public class UploadHistoryFragment extends BaseFragment implements CarBrandPop.CarBrandSelectListener, SpringView.OnFreshListener, MultiItemTypeAdapter.OnItemClickListener{
    @InjectView(tv_car_brand)
    TextView carBrandTv;
    @InjectView(R.id.recycler_view)
    RecyclerView recyclerView;
    @InjectView(R.id.spring_view)
    SpringView springView;
    private CarBrandPersenter carBrandPersenter;
    private List<ToDayBean> data = null;
    private List<SelectCarBrandBean> brandList;
    private UploadHistoryAdapter uploadHistoryAdapter;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        contentView = inflater.inflate(R.layout.fragment_upload_history, null);
        ButterKnife.inject(this, super.onCreateView(inflater, container, savedInstanceState));
        initViews();
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    private void initViews() {
        springView.setListener(this);
        springView.setHeader(new DefaultFooter(getActivity()));
        springView.setFooter(new DefaultFooter(getActivity()));
        springView.setType(SpringView.Type.FOLLOW);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(layoutManager);
        reuestCarBrand();
        getData("");
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        Logger.d("UploadHistoryFragment");

    }
    private void getData(String brandId) {
        HashMap<String,Object> map = new MapUtil().
                put("brand_id",brandId).build();
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(getActivity(), map).create(IApiFragmentRequest.class).uploadHistory(),
                new BaseSubscriber<TokenBean<List<ToDayBean>>>(getActivity()) {
                    @Override
                    public void onSuccess(TokenBean<List<ToDayBean>> o) {
                        springView.onFinishFreshAndLoad();
                        if(o.getData()!=null && o.getData().size() >0){
                            refreshData(o.getData());
                        }else{
                            showToast("没有数据！");
                        }
                    }
                    @Override
                    public void onFailure(Throwable e) {
                        springView.onFinishFreshAndLoad();
                    }
                });
    }
    private void refreshData(List<ToDayBean> data) {
        this.data = data;
        if(uploadHistoryAdapter == null){
            uploadHistoryAdapter = new UploadHistoryAdapter(getActivity(), R.layout.layout_item_upload_history, data);
            uploadHistoryAdapter.setOnItemClickListener(this);
            recyclerView.setAdapter(uploadHistoryAdapter);
        }else{
            uploadHistoryAdapter.refresh(data);
        }
    }
    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @OnClick({tv_car_brand})
    public void onClick(View view) {
        switch (view.getId()) {
            case tv_car_brand:     //跳转车品牌
//                CarBrandPop carBrandPop = new CarBrandPop(getActivity(),brandList,this) ;
//                carBrandPop.showAsDropDown(carBrandTv,-180,0, Gravity.LEFT);
                break;
        }
    }

    @Override
    public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {

    }

    @Override
    public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
        return false;
    }



    public void reuestCarBrand() {
        RxRequest.request(ShowType.SHOW, RxRequest.getClient(getActivity(), new MapUtil().build()).create(IApiActivityRequest.class).getCarBrand(),
                new BaseSubscriber<TokenBean<List<SelectCarBrandBean>>>(context) {
                    @Override
                    public void onSuccess(TokenBean<List<SelectCarBrandBean>> o) {
                        sortCarBrand(o.getData());
                    }

                    @Override
                    public void onFailure(Throwable e) {

                    }
                });
    }

    public void sortCarBrand(List<SelectCarBrandBean> brandList) {
        //按字母排序-yd
        Collections.sort(brandList, new Comparator<SelectCarBrandBean>(){
            public int compare(SelectCarBrandBean carBrandBean1, SelectCarBrandBean carBrandBean2) {
                if(carBrandBean1.getFirstLetter().charAt(0) > carBrandBean2.getFirstLetter().charAt(0)){
                    return 1;
                }
                if(carBrandBean1.getFirstLetter().charAt(0) == carBrandBean2.getFirstLetter().charAt(0)){
                    return 0;
                }
                return -1;
            }
        });
        this.brandList = brandList;
        LogUtils.e(brandList.toString());

    }
    @Override
    public void onRefresh() {
        getData(brandId);
    }

    @Override
    public void onLoadmore() {
        springView.onFinishFreshAndLoad();
    }

    @Override
    public void getBrandId(String brandId,String brandName) {
        getData(brandId);
        carBrandTv.setText(brandName);
    }
}
